﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QiAdmin.Core
{
    public class CommonResult
    {
        public ResultType Code { get; set; }
        public string? Msg { get; set; }

        public CommonResult(ResultType code, string? msg)
        {
            Code = code;
            Msg = msg;
        }
        public static CommonResult Success => new CommonResult(ResultType.Success, null);
        public static CommonResult Error => new CommonResult(ResultType.Error, null);
        public static CommonResult Warning => new CommonResult(ResultType.Warning, null);
        public static CommonResult Convert(bool exp, string msg = null)
        {
            return new CommonResult(exp ? ResultType.Success : ResultType.Error, msg);
        }
        public bool IsSuccess => Code == ResultType.Success;
    }

    public class CommonResult<T> : CommonResult
    {
        public T Result { get; set; }
        public CommonResult(ResultType code, string? msg, T result)
           : base(code, msg)
        {
            Result = result;
        }
        public CommonResult(T result)
          : this(ResultType.Success, null, result)
        {
        }
        public static CommonResult<T> Success => new CommonResult<T>(default);
        public static CommonResult<T> Error => new CommonResult<T>(ResultType.Error, null, default);
        public static CommonResult<T> Warning => new CommonResult<T>(ResultType.Warning, null, default);
        public static CommonResult<T> Convert(bool exp, string msg = null)
        {
            return new CommonResult<T>(exp ? ResultType.Success : ResultType.Error, msg, default);
        }
    }
}
